﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OhalcLib;
using System.Threading;
using System.Globalization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ManualProject
{
    class Program
    {
        public enum Places { Jp, Eu, Us}

        

        static void Main(string[] args)
        {
            string filePath = "EURUSD5.csv";
            string line = "";
            //Tick[] ticks=new Tick[];
            List<Session> sessions = new List<Session>();
            List<Tick> ticks = new List<Tick>();

            if (File.Exists(filePath))
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

                StreamReader file = new StreamReader(filePath);
                string[] splitter = new string[] { "," };

                string[] objs;
                Tick tick = new Tick();

                int e = 0;
                while ((line = file.ReadLine()) != null)
                {
                    objs = line.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
                    tick = new Tick();

                    tick.Time = Convert.ToDateTime(String.Format("{0} {1}",objs[0],objs[1]));
                    tick.Open = Math.Round(Convert.ToDecimal(objs[2]), 5);
                    tick.High = Math.Round(Convert.ToDecimal(objs[3]), 5);
                    tick.Low = Math.Round(Convert.ToDecimal(objs[4]), 5);
                    tick.Close = Math.Round(Convert.ToDecimal(objs[5]), 5);

                    //if(e<1000)
                        ticks.Add(tick);
                    //Console.WriteLine(line);
                    e++;
                }

                Stream stream = File.Open("Ticks.xml", FileMode.Create);

                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(ticks.GetType());
                x.Serialize(stream, ticks);
                stream.Close();



                DateTime curDate = ticks[0].Time.Date;
                
               

                TimeSpan jpStart=new TimeSpan(1,0,0);
                TimeSpan jpEnd=new TimeSpan(7,55,0);
                TimeSpan euStart=new TimeSpan(8,0,0);
                TimeSpan euEnd=new TimeSpan(15,25,0);
                TimeSpan usStart=new TimeSpan(15,30,0);
                TimeSpan usEnd=new TimeSpan(22,55,0);

                decimal open = 0;
                SessionPoint high = new SessionPoint();
                SessionPoint low = new SessionPoint();
                decimal close = 0;
                //decimal width = 0;
                //decimal avg = 0;
                //decimal avgShift = 0;
                //string type = "";

                Session session = new Session();

                if (ticks[0].Time.TimeOfDay >= jpStart && ticks[0].Time.TimeOfDay <= jpEnd)
                {
                    open = ticks[0].Open;
                    high.Point=ticks[0].Open;
                    high.TimeOfDay = ticks[0].Time.TimeOfDay;
                    low.Point = ticks[0].Open;
                    low.TimeOfDay=ticks[0].Time.TimeOfDay;
                }
                if (ticks[0].Time.TimeOfDay >= euStart && ticks[0].Time.TimeOfDay <= euEnd)
                {
                    open = ticks[0].Open;
                    high.Point = ticks[0].Open;
                    high.TimeOfDay = ticks[0].Time.TimeOfDay;
                    low.Point = ticks[0].Open;
                    low.TimeOfDay = ticks[0].Time.TimeOfDay;
                }
                if (ticks[0].Time.TimeOfDay >= usStart && ticks[0].Time.TimeOfDay <= usEnd)
                {
                    open = ticks[0].Open;
                    high.Point = ticks[0].Open;
                    high.TimeOfDay = ticks[0].Time.TimeOfDay;
                    low.Point = ticks[0].Open;
                    low.TimeOfDay = ticks[0].Time.TimeOfDay;
                }

                
                DateTime changeGTTime=new DateTime(2011,4,30,0,0,0);


                for (int i = 0; i < ticks.Count; i++)
                {

                    if (ticks[i].Time > changeGTTime)
                    {
                        jpStart = new TimeSpan(2, 0, 0);
                        jpEnd = new TimeSpan(8, 55, 0);
                        euStart = new TimeSpan(9, 0, 0);
                        euEnd = new TimeSpan(16, 25, 0);
                        usStart = new TimeSpan(16, 30, 0);
                        usEnd = new TimeSpan(22, 55, 0);
                    }

                    if (i == ticks.Count - 13)
                    {
                        int j = i;
                    }

                    if (high.Point < ticks[i].High)
                    {
                        high.Point = ticks[i].High;
                        high.TimeOfDay = ticks[i].Time.TimeOfDay;
                    }

                    if (low.Point > ticks[i].Low)
                    {
                        low.Point = ticks[i].Low;
                        low.TimeOfDay = ticks[i].Time.TimeOfDay;
                    }

                    if (ticks[i].Time.TimeOfDay == jpStart )
                    {
                        session = new Session();
                        open = ticks[i].Open;
                        high = new SessionPoint();
                        low = new SessionPoint();
                        high.Point = ticks[i].Open;
                        high.TimeOfDay = ticks[i].Time.TimeOfDay;
                        low.Point = ticks[i].Open;
                        low.TimeOfDay = ticks[i].Time.TimeOfDay;
                        close = 0;
                    }
                    if (ticks[i].Time.TimeOfDay == jpEnd)
                    {
                        close = ticks[i].Close;
                        session = MakeSession(i, "jp", session, ticks, sessions, open, high, low, close);
                        sessions.Add(session);
                    }
                    if (ticks[i].Time.TimeOfDay == euStart)
                    {
                        session = new Session();
                        open = ticks[i].Open;
                        high = new SessionPoint();
                        low = new SessionPoint();
                        high.Point = ticks[i].Open;
                        high.TimeOfDay = ticks[i].Time.TimeOfDay;
                        low.Point = ticks[i].Open;
                        low.TimeOfDay = ticks[i].Time.TimeOfDay;
                        close = 0;
                    }
                    if (ticks[i].Time.TimeOfDay == euEnd)
                    {
                        close = ticks[i].Close;
                        session = MakeSession(i, "eu", session, ticks, sessions, open, high, low, close);
                        sessions.Add(session);
                    }
                    if (ticks[i].Time.TimeOfDay == usStart)
                    {
                        session = new Session();
                        open = ticks[i].Open;
                        high = new SessionPoint();
                        low = new SessionPoint();
                        high.Point = ticks[i].Open;
                        high.TimeOfDay = ticks[i].Time.TimeOfDay;
                        low.Point = ticks[i].Open;
                        low.TimeOfDay = ticks[i].Time.TimeOfDay;
                        close = 0;
                    }
                    if (ticks[i].Time.TimeOfDay == usEnd)
                    {
                        close = ticks[i].Close;
                        session = MakeSession(i, "us", session, ticks, sessions, open, high, low, close);
                        sessions.Add(session);
                    }
                }

                Stream stream1 = File.Open("Sessions.xml", FileMode.Create);

                System.Xml.Serialization.XmlSerializer x1 = new System.Xml.Serialization.XmlSerializer(sessions.GetType());
                x1.Serialize(stream1, sessions);
                stream1.Close();


                string toReturn = "";

                //string pr3 = "OLAHC;OHALC;OLAHC";
                string pr3 = "OLAHC;OHALC;OHALC";

                List<Session> s1 = sessions.FindAll(y => y.prev3 == pr3);
                String ans = "";

                List<string> listFoAnswers = new List<string>();

                foreach (Session s in s1)
                {
                    if (!listFoAnswers.Contains(s.Type))
                    {
                        decimal percent = 0;
                        
                        decimal avgWidth = 0;
                        List<Session> sDetails = s1.FindAll(z => z.Type == s.Type);
                        avgWidth =Math.Round( (sDetails.Average(avg => avg.Width)),2);
                        decimal occasions = (decimal)sDetails.Count / s1.Count;
                        percent = Math.Round(occasions,4) * 100;
                        ans = String.Format("Type: {0}, occasions {1}%, average width: {2} points", s.Type, percent,avgWidth.ToString());
                        listFoAnswers.Add(s.Type);

                        Console.WriteLine(ans);
                    }
                }
                Console.ReadLine();
            }

            
            
        }

       static  public Session MakeSession(int i, string place, Session session, List<Tick> ticks, List<Session> sessions, decimal open, SessionPoint high, SessionPoint low, decimal close)
        {
            session.Date = ticks[i].Time.Date;
            session.Place = place;
            session.Open = open;
            session.High = high.Point;
            session.Low = low.Point;
            session.Close = ticks[i].Close;
            session.Width = Math.Abs(high.Point - low.Point)*10000;

            decimal avg=0;

            if (high.Point > low.Point)
                avg = high.Point - ((high.Point - low.Point) / 2);
            if (high.Point < low.Point)
                avg = low.Point - ((low.Point - high.Point) / 2);
            if (high == low)
                avg = high.Point;

            session.Avg=avg;

            if (sessions.Count > 0)
                session.AvgShift = avg - sessions[sessions.Count - 1].Avg;

            string type="";

            if (open < avg && close > avg)
            {
                if (high.TimeOfDay > low.TimeOfDay)
                    type = "OLAHC";
                if (high.TimeOfDay < low.TimeOfDay)
                    type = "OAHALAC";
                if (high.TimeOfDay == low.TimeOfDay)
                    type = "OLAHC_ONE";
            }
            if (open > avg && close > avg)
            {
                if (high.TimeOfDay > low.TimeOfDay)
                    type = "OALAHC";
                if (high.TimeOfDay < low.TimeOfDay)
                    type = "OHALAC";
                if (high.TimeOfDay == low.TimeOfDay)
                    type = "OALAHC_ONE";
            }
            if (open > avg && close < avg)
            {
                if (high.TimeOfDay > low.TimeOfDay)
                    type = "OALAHAC";
                if (high.TimeOfDay < low.TimeOfDay)
                    type = "OHALC";
                if (high.TimeOfDay == low.TimeOfDay)
                    type = "OHALC_ONE";
            }
            if (open < avg && close < avg)
            {
                if (high.TimeOfDay > low.TimeOfDay)
                    type = "OLAHAC";
                if (high.TimeOfDay < low.TimeOfDay)
                    type = "OAHALC";
                if (high.TimeOfDay == low.TimeOfDay)
                    type = "OAHALC_ONE";
            }

            session.Type = type;

           if(sessions.Count>3)
               session.prev3 = sessions[sessions.Count - 3].Type + ";" + sessions[sessions.Count - 2].Type + ";" + sessions[sessions.Count - 1].Type;

           //if (sessions.Count > 4)
           //    sessions[sessions.Count - 1].next1 = session.Type;

            return session;
        }
    }
}
